﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Collections.Specialized;

//This class is used to maitain queries related to Slabs, which are used to populate slabs
public sealed class SlabsDisplay
{
    #region create threadsafe singleton
    private static volatile SlabsDisplay instance;
    private static object syncRoot = new Object();

    private SlabsDisplay() { }

    public static SlabsDisplay Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new SlabsDisplay();
                }
            }
            return instance;
        }
    }
    #endregion

    #region drop down lists
    public static List<String> SteppedTiered()
    {
        List<String> lt = new List<String>();
        lt.Add("Stepped");
        lt.Add("Tired");
        lt.Add("Flat");
        return lt;
    }

    public static String formulaListQuery()
    {
        return "SELECT [RowNo], [Name] FROM [Formula] WHERE ([Type] = 'Formula') AND ([SubType] in( 'Slabs','Generic')) ORDER BY [RowNo]";
    }

    public static List<String> Operators()
    {
        List<String> lt = new List<String>();
        lt.Add("<");
        lt.Add(">");
        lt.Add("<=");
        lt.Add(">=");
        lt.Add("=");
        lt.Add("LIKE");
        return lt;
    }

    public static List<String> Dimensions()
    {
        List<String> lt = new List<String>();
        for (int i = 1; i <= 9; i++)
        {
            lt.Add(i.ToString());
        }
        return lt;
    }
    #endregion

    #region slab details
    public static String getSingleSlabQuery(int SlabId)
    {
        String query = "SELECT [Name],[FromDate],[ToDate],[Dimension],[ROperator],[COperator], [IsStepped], [RFormula], [CFormula], [CreatedBy], [CreatedOn] ";
        query +=    "FROM [Slabs] WHERE [RowNo]='" + SlabId.ToString() + "'";

        return query;
    }

    public static NameValueCollection getNVC4Slabs()
    {
        NameValueCollection nvc = new NameValueCollection();
        //nvc.Add("RowNo", "");
        nvc.Add("Name", "");
        nvc.Add("FromDate", "");
        nvc.Add("ToDate", "");
        nvc.Add("Dimension", "");
        nvc.Add("ROperator", "");
        nvc.Add("COperator", "");
        nvc.Add("IsStepped", "");
        nvc.Add("RFormula", "");
        nvc.Add("CFormula", "");
        nvc.Add("CreatedBy", GlobalVariables.loginId);
        nvc.Add("CreatedOn", DateTime.Now.ToString());
        return nvc;
    }

    public static DataRow getSlabRow(int SlabId)
    {
        string selectQuery = SlabsDisplay.getSingleSlabQuery(SlabId);
        DataTable dt = DataBaseMethods.ExecuteSelectQuery(selectQuery);
        if (dt.Rows.Count != 1)
        {
            Logger.Show(Logger.SLABS_NOT_EXIST + "ID = " + SlabId.ToString());
            return null;
        }
        else
        {
            return dt.Rows[0];
        }
    }
    #endregion
}
